/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "msgServ", "ajaxServ","formServ","domReady"], 
	function($,msgServ,ajaxServ,formServ,domReady) {

	'use strict';

	var moduleEl, formEl, private_method = 0,public_method=0, formData = 0, homeUrl = '';
	var public_method={
			/**
		 * 
		 * 登录按钮点击handle
		 * 
		 * @param {Event}
		 *            典型 DOM event 对象
		 * @param {HTMLElement}
		 *            element The nearest HTML element with a data-type
		 *            attribute specified or null if there is none.
		 * @param {string}
		 *            elementType The value of data-type for the nearest element
		 *            with that attribute specified or null if there is none.
		 * @returns {void}*
		 */
		loginclick:function(event) {			
			
			if(event)
				event.preventDefault();
				
			formEl = $('form');
			
			formData = formServ.getDataObj({
				form : formEl
			});

			var rules, messages, validateOptions;

			rules = {
				username : "required"
			};
			messages = {
				username : '用户名不能为空'
			};
			validateOptions = {
				debug : true,
				targetForm : formEl,
				rules : rules,
				messages : messages,
				submitHandler : private_method.loginHandler
			};

			formServ.onSubmit(validateOptions);

				// isValid = formServ.validate(validateOptions);

				// context.broadcast('todostatuschange');
		}
	}
	
	
    
	var private_method = {
		loginHandler : function(event) {

			var _opt = {
				url : 'biz/login',
				data : formData,
				callback : private_method.loginOK,
				contentType : 'form'
			};

			ajaxServ.runAjax(_opt);

		},
		loginOK : function(data, ressionStr, statusObj) {

			if (statusObj) {
				switch (statusObj.status) {
					case 200 :						
						window.location.href = data.retStr;
						break;
					case '200' :
						window.location.href = data.retStr;
						break;
					case 204 :
						msgServ.Msgslide("用户名或密码错误");
						break;
					case '204' :
						msgServ.Msgslide("用户名或密码错误");
						break;

				}

			}
			// alert('login ok !!!');
		},
		splitSite:function(siteUrl){			
			
			if(siteUrl){
				var siteArr = siteUrl.split('/');
				var site =  siteArr[0];
				return site;
			}
			
			return
			
		},
		formPost: function(URL, PARAMS) {        
		    var temp = document.createElement("form");        
		    temp.action = URL;        
		    temp.method = "post";        
		    temp.style.display = "none"; 
		    var _html
		    for (var x in PARAMS) {	            
		        var para = document.createElement("input");
		        para.innerHtml="<input type='text' id='" + x +"' value='"+ PARAMS[x] +"'>";
		        // alert(opt.name)        
		        temp.appendChild(para);        
		    }        
		    document.body.appendChild(temp);        
		    temp.submit();        
		    return temp;        
		}    

	};
	
		

		
	domReady(function (param1,param2) {
        console.info('login modules gets ready and  been loaded.');
        
		
		
		 $("a.a-btn").bind('click',function(){
		 	public_method.loginclick();
		 });
    });
		
	return public_method;

	

});